<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Category;
use App\Http\Model\Video;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;

class VideoController extends Controller
{
    public function addVideo(){
        check_auth();
        if($input=Input::except('_token')){
            $input['user_id']=Session::get('admin')['id'];
            if(Video::create($input)){
                return ['status'=>1,'msg'=>'保存完成'];
            }else{
                return ['status'=>0,'msg'=>'保存失败'];
            }
        }
        $category=Category::where(['type'=>3,'status'=>1])->get()->all();
        return view('admin.video.addvideo',compact('category'))->with(['title'=>'添加视频']);
    }

    public function editVideo()
    {
        
    }

    public function videoList($status){
        check_auth();
        $user_id=Session::get('admin')['id'];
        $endTime=$startTime=$titler=$category_id='';
        $videolist=Video::select('videos.title','videos.time','videos.cover','videos.views','videos.created_at','videos.id','videos.status','category.category')
            ->join('category','category.id','=','videos.category_id','left')->orderBy('created_at','desc')->where(['videos.status'=>$status,'videos.user_id'=>$user_id]);
        if($search=Input::except('page')){
            $startTime=$search['start_time'];
            $endTime=$search['end_time'];
            $titler=$search['title'];
            $category_id=$search['category_id'];
            if(!empty($category_id)){
                $videolist=$videolist->where('category_id','=',$category_id);
            }
            if(!empty($titler)){
                $videolist=$videolist->where('videos.title','like',"%".$titler."%");
            }
            if(!empty($startTime)){
                $videolist=$videolist->where('videos.created_at','>',strtotime($startTime));
            }
            if(!empty($endTime)){
                $endTimer=strtotime($endTime)+3600*24;
                $videolist=$videolist->where('videos.created_at','<',$endTimer);
            }
            $videolist=$videolist->paginate(3)->appends(['title' => $titler,'start_time'=>$startTime,'end_time'=>$endTime,'category_id'=>$category_id]);
        }else{
            $videolist=$videolist->paginate(3);
        }
        $category=Category::where(['type'=>3])->get()->all();
        return $status?view('admin.video.videolist',compact('videolist','category','titler','category_id','startTime','endTime'))->with(['title'=>'视频列表']):
            view('admin.video.recyclebin',compact('videolist','category','titler','category_id','startTime','endTime'))->with(['title'=>'视频回收站']);
    }

    public function updateVideoStatus(){
        check_auth();
        $user_id=Session::get('admin')['id'];
        if($input=Input::all()){
            $status=$input['status']?0:1;
            if(Video::where(['id'=>$input['id'],'user_id'=>$user_id])->update(['status'=>$status])){
                return ['status'=>1,'msg'=>'修改完成'];
            }else{
                return ['status'=>0,'msg'=>'修改失败'];
            }
        }
    }

    /*批量更改状态*/
    public function multiUpdateVideoStatus(){
        check_auth();
        $user_id=Session::get('admin')['id'];
        if($selected=Input::all()){
            $success='';
            $false='';
            foreach($selected['selected'] as $id){
                if(Video::where(['id'=>$id,'user_id'=>$user_id])->update(['status'=>$selected['status']])){
                    $success.=$id.',';
                }else{
                    $false.=$id.',';
                }
            }
            return empty($false)?['status'=>1,'msg'=>$success.'已操作完成']:['status'=>1,'msg'=>$success.'操作完成,'.$false.'操作失败'];
        }
    }

    /*删除*/
    public function deleteVideo(){
        check_auth();
        if($input=Input::all()){
            return $this->delete($id=$input['id']);
        }
    }

    private function delete($id){
        $msg=$id.':';
        $user_id=Session::get('admin')['id'];
        $images=Video::select('cover')->find($id);
        if(Video::where(['id'=>$id,'user_id'=>$user_id])->delete()){
            $msg.='视频已删除,';
            $status=1;
            $disk = Storage::disk('qiniu');
            if(!empty($images->cover)){
                $cover=basename($images->cover);
                if($disk->exists($cover)){
                    if($disk->delete($cover)){
                        $msg.="封面图已经删除,";
                    }else{
                        $msg.="封面未删除,";
                    }
                }else{
                    $msg.="封面不存在,";
                }
            }
        }else{
            $status=0;
            $msg.='视频未删除';
        }
        return ['status'=>$status,'msg'=>$msg];
    }

    /*批量删除*/
    public function multiDeleteVideo(){
        check_auth();
        if($selected=Input::all()){
            $success='';
            $false='';
            foreach($selected['selected'] as $id){
                $res=$this->delete($id);
                if($res['status']){
                    $success.=$id.',';
                }else{
                    $false.=$id.',';
                }
            }
            return empty($false)?['status'=>1,'msg'=>$success.'已操作完成']:['status'=>1,'msg'=>$success.'操作完成,'.$false.'操作失败'];
        }
    }

    /*修改*/
    public function editVideos($id){
        check_auth();
        $user_id=Session::get('admin')['id'];
        $videoInfo=Video::where(['user_id'=>$user_id])->find($id);
        $category=Category::where(['type'=>3])->get()->all();
        return view('admin.video.editvideo',compact('category','videoInfo'))->with(['title'=>'修改视频']);
    }

    public function updateVideo(){
        check_auth();
        $user_id=Session::get('admin')['id'];
        if($input=Input::except('_token')){
            $id=$input['id'];
            $oldArticle=Video::where(['user_id'=>$user_id])->find($id);
            $disk = Storage::disk('qiniu');
            if($oldArticle['cover']!=$input['cover']){
                //图片更新了，删除旧图
                if(!empty($oldArticle['cover'])){
                    $oldCover=basename($oldArticle->cover);
                    if($disk->exists($oldCover)){
                        $disk->delete($oldCover);
                    }
                }
            }
            unset($input['id']);
            if(Video::where(['id'=>$id,'user_id'=>$user_id])->update($input)){
                return ['status'=>1,'msg'=>'更新完毕'];
            }else{
                return ['status'=>0,'msg'=>'更新失败'];
            }
        }
    }
}
